package com.ruoyi.web.controller.delivery;

import com.jianqiao.delivery.delivery.domain.DeliveryEntity;
import com.jianqiao.delivery.delivery.service.DeliveryService;
import com.jianqiao.delivery.delivery.service.OcrService;
import com.jianqiao.delivery.delivery.service.ResolveAddressService;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @description:  图片文字识别控制类
 * @author: lsx
 * @create: 2024-11-25 14:36
 **/
@RestController
@RequestMapping("/ocr")
@Slf4j
public class OcrController {
    @Autowired
    private OcrService ocrService;

    @Autowired
    private ResolveAddressService resolveAddressService;

    @Autowired
    private DeliveryService deliveryService;

    public OcrController(OcrService ocrService) {
        this.ocrService = ocrService;
    }

    @PostMapping(value = "/recognize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult recognizeImage(@RequestParam("file") MultipartFile file) throws TesseractException, IOException {
        // 调用OcrService中的方法进行文字识别
        String address = ocrService.recognizeText(file);
        log.info(address);
        // 正则拆分
//        Map resultMap = ocrService.Address(address);
//        return AjaxResult.success(resultMap);

//        address = address.replace("." , "");
        AjaxResult resultMap = resolveAddressService.resolveJpAddressService(address);
//        AjaxResult resultMap = resolveAddressService.resolveIndAddressService(address);
        System.out.println(resultMap);
        return resultMap;
    }

    @PostMapping("/scanNumber")
    public AjaxResult scanNumber(@RequestParam("file") MultipartFile file) throws TesseractException, IOException {
        String number = ocrService.recognizeText(file);
        List<DeliveryEntity> deliveryList = deliveryService.listByOrderNumber(number);
        return AjaxResult.success(deliveryList);
    }
}
